package com.jqb.hibernatebbs.util;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * 配合sessionOpenInView解决方案 ,所有session都使用线程级
 * 
 * @author Dong
 * 
 */
public class SessionLazyHandler {

	private static SessionFactory sessionFactory = null;
	private static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();;

	private SessionLazyHandler() {
	}

	static {
		System.out.println("SessionFactory initing...");
		sessionFactory = new Configuration().configure().buildSessionFactory();
		System.out.println("SessionFactory init over...");
	}
	//仅供测试(二级缓存命中率)
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	// 获取线程级session
	public static Session getCurrentSession() {
		Session session = null;
		try {
			session = threadLocal.get();
			if (session == null) {
				session = sessionFactory.openSession();
				threadLocal.set(session);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return session;
	}

	// 关闭session,并且把线程变量置空
	public static void closeCurrentSession() {
		Session session = null;
		try {
			session = threadLocal.get();
			if (session != null && session.isOpen()) {
				session.close();
				threadLocal.set(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 更新[删除]
	public static void hqlUpdate(String hql, String[] params) {
		Session session = getCurrentSession();
		Query query = session.createQuery(hql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				query.setString(i, params[i]);
			}
		}
		query.executeUpdate();
	}
	//添加
	public static void hqlInsert(Object obj) {
		Session session = getCurrentSession();
		session.save(obj);
	}
	//查询
	public static List<?> hqlQuery(String hql, String[] params) {
		List<?> list = null;
		Session session = getCurrentSession();
		Query query = session.createQuery(hql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				query.setString(i, params[i]);
			}
		}
		list = query.list();
		return list;
	}
	//查询[带分页]
	public static List<?> hqlPagingQuery(String hql, String[] params,
			int pageSize, int pageNum) {
		List<?> list = null;
		Session session = getCurrentSession();
		Query query = session.createQuery(hql);
		query.setFirstResult((pageNum - 1) * pageSize);
		query.setMaxResults(pageSize);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				query.setString(i, params[i]);
			}
		}
		list = query.list();
		return list;
	}
	//查询[ID]
	public static Object hqlQueryById(Class<?> clazz,Serializable i){
		Object obj = null;
		Session session = getCurrentSession();
		obj = session.load(clazz, i);
		return obj;
	}
	
	//查询[Unique]
	public static Object hqlQueryByUniqueString(String hql, String uniqueString){
		Object obj = null;
		Session session = getCurrentSession();
		Query query = session.createQuery(hql);
		query.setString(0, uniqueString);
		obj = query.uniqueResult();
		return obj;
	}
}
